

cd "EnnsLagodny_PS_Dataverse\"

use "EnnsLagodny_PS_Forecast2020.dta", clear

xtset state_initnum year, delta(4)

*********************
***Table 1
*********************
reg PerDem2Party l.PerDem2Party_dev pres_app CEI ///
democrat_p_state l.democrat_p_state ///
democrat_vp_state south_conf Anderson Perot2 
*********************


***********************
***Before-the-fact Forecasts
***********************
	gen yhatall = .
	
forvalues i = 1984(4)2020 {
		local drop all
		local yr=`i'

reg PerDem2Party l.PerDem2Party_dev pres_app CEI ///
south_conf Anderson Perot2 democrat_p_state l.democrat_p_state ///
democrat_vp_state if year>1976 & year<`yr'

		predict yhat`yr' if year==`yr'
		replace yhatall = yhat`yr' if year==`yr'
}

gen correct_forecast = 0
recode correct_forecast 0=1 if PerDem2Party<50 & yhatall<50 & yhatall!=. | PerDem2Party>50 & yhatall>50 &  yhatall!=.		
******************************************
***Percent of States Correctly forecasted
******************************************
tab correct_forecast if year>=1984 & year<=2016
******************************************
***Percent Correctly forecasted Since 2000
******************************************
tab correct_forecast if year>2000 & year<=2016


********************************************
*Figure 1
********************************************
*DC in 1984 has a prediction >100%
replace yhatall = 100 if yhatall>100 & yhatall!=.
list cstate_initial year yhatall if yhatall==100 & yhatall!=.

graph set window fontface "Times New Roman"
twoway scatter yhatall PerDem2Party, msize(vtiny) mcolor(black) xsize(5) ysize(5) ///
yscale(r(0 105) axis(1) noextend lwidth(.01)) ylab(0(25)100, nogrid)    ///
xscale(r(0 101) lwidth(.01)) xlabel(0(25)100, nogrid) ///
xtitle("Actual Democratic Two-Party Vote Share") ytitle("Before-the-Fact Forecasted" "Democratic Two-Party Vote Share") /// 
yline(50, lcolor(black)) xline(50, lcolor(black)) || lfit PerDem2Party PerDem2Party,  ///
range(1 99)  /// 
lcolor(gs10) legend(off)  ///
plotregion(margin(l=0 r=1 b=0 t=-4) lcolor(black)) ///
graphregion(fcolor(white) lcolor(white))
*******************************************


********************************
*Table 2
*Enns & Lagodny State Forecast Values
********************************
gen abs_resall = abs(yhatall - PerDem2Party)

sum abs_resall if year==2012, d
sum abs_resall if year==2016, d
********************************


**********************************
*2020 EC Prediction
**********************************
gen dem_win= 1 if yhatall>50 & year==2020
gen dem_state_ECV= total_electoral*dem_win if year==2020

egen dem_national_ECV=sum(dem_state_ECV) if year==2020
tab dem_national_ECV
**********************************

********************************
*2020 National Vote Prediction
********************************
keep if year==2020
keep  yhatall cstate_name cstate_initial
* collapse so only one line is available per state (basically making data set "wide"
collapse (max) yhatall, by(cstate_name cstate_initial)
* merge the relative population size
merge 1:1 cstate_name using "relative_population_size2020.dta"
drop _merge
	* multiply democrat 2 party vote * relative population size
	gen dem_strength= yhatall*rel_2020
	* add up all weighted democrat 2 party vote strengths
	egen national_yhat2020= sum(dem_strength)
	* tabulate for all years
	tab national_yhat2020

